<?php
	/**
	*	Class: Common - common function
	*
	*	Date(dd-mm-yyy):
	*
	*	Created:
	*/
	
	function dump() {
		ob_start();
		var_dump(func_get_args());
		$my_string = ob_get_contents();
		ob_end_clean();

		return $my_string;
	}	
	
	function checkType($var) {
		
		if(is_object($var)) {
        	return get_class($var);
        }
	    
	    if(is_null($var)) {
	        return 'null';
	    }
	    
	    if(is_string($var)) {
	        return 'string';
	    }
	        
	    if(is_array($var)) {
	        return 'array';
	    }
	    
	    if(is_int($var)) {
	        return 'integer';
	    }
	        
	    if(is_bool($var)) {
	        return 'boolean';
	    }
	        
	    if(is_float($var)) {
	        return 'float';
	    }
	        
	    if(is_resource($var)) {
	        return 'resource';
	    }
	    return 'unknown'; 
	}
	
	function jsonEncode($json, $assoc = false) {
		return json_encode($json, $assoc);
	}
	
	function jsonDecode($value, $options = false) {
		return json_decode($value, $options);
	}
	
	function isPost() {
		return strtoupper($_SERVER['REQUEST_METHOD']) == "POST" ? true : false;
	}
	
	function isGet() {
		return strtoupper($_SERVER['REQUEST_METHOD']) == "GET" ? true : false;
	}


	function getParam($var) {
		if(isset($_POST[$var])) {
			return $_POST[$var];
		}
		else if(isset($_GET[$var])) {
			return $_GET[$var];
		}
		return "";
	}
	
	function getClientIP() {
		return $_SERVER['REMOTE_ADDR'];
	}
	
	function redirect($url) {
		echo "<meta http-equiv='refresh' content='0;url=$url'>";
	}
	
	function toString($array, $separator = ',') {
		return implode($separator, $array);
	}
	
	function toArray($str, $separator = ',') {
		return explode($separator, $str);
	}
	
	function getLength($var) {
		
		$type = checkType($var);
		
		switch($type) {
			case "string":
				return strlen(trim($var));
			case "array":
				return sizeof($var);
		}
		
		return -1;
	}
	
	function findItem($var, $item) {
		
		$type = checkType($var);
		
		switch($type) {
			case "string":
				return strstr($var, $item);
			case "array":
				return isset($var[$item]) || in_array($item, $var) || array_search($item, $var);
		}
		
		return false;
	}
	
	function getPageSort() {
	
		$recPerPage = getParam('limit'); $recPerPage > 0 ? $recPerPage : 25;
		$curPage = getParam('start'); $curPage > 0 ? $curPage : 1;
		$sortField = getParam('sort');
		$direction = getParam('dir'); $direction != '' ? $direction : 'asc';
		
		$curPage = ($curPage / $recPerPage) + 1;
		
		$params = array(
			size => $recPerPage,
			index => $curPage,
			field => $sortField,
			type => $direction
		);
	
		return $params;
	}
	
	function getLimitSort() {
	
		$recPerPage = getParam('limit'); $recPerPage > 0 ? $recPerPage : 25;
		$curPage = getParam('start'); $curPage > 0 ? $curPage : 0;
		$sortField = getParam('sort');
		$direction = getParam('dir'); $direction != '' ? $direction : 'asc';
			
		$params = array(
			limit => $recPerPage,
			start => $curPage,
			field => $sortField,
			type => $direction
		);
		
		return $params;
	}

	
		/*
	 * @return array password hashed and salt
	 * need when create new account
	 */ 
	function password_hashed($input_password){
		$random_salt = D_Utils::random_string();
		$stretches = 5;
		
		$result = array(
			'encrypted_password' => digest($input_password, $stretches, $random_salt),
			'password_salt' =>  $random_salt
		);
	
		return $result;
	}
	
	
	
	// protected function
	// encrypt algoriml
	
	
	/*
	 * @return password_hashed
	 * 
	 */
	function digest($password, $stretches, $salt, $pepper=''){ 
		$digest = $pepper;
		for($i=0; $i< $stretches; $i++){
		  $digest = secure_digest($salt, $digest, $password, $pepper);
		}
		return $digest;
	} 
	
	function secure_digest($salt, $digest, $password, $pepper){
		$hash_join = $salt."--".$digest."--".$password."--".$pepper;
		return sha1("--".$hash_join."--");
	}
	
function change_code2($str){
	   if(!$str) return false;
	   $unicode = array(
	   'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ',
	   'A'=>'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
      'd'=>'đ',
	  'D'=>'Đ',
      'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
	  'E'=>'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
      'i'=>'í|ì|ỉ|ĩ|ị',
	  'I'=>'Í|Ì|Ỉ|Ĩ|Ị',
      'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
	  'O'=>'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
      'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
	  'U'=>'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
      'y'=>'ý|ỳ|ỷ|ỹ|ỵ',
	  'Y'=>'Ý|Ỳ|Ỷ|Ỹ|Ỵ',
	   '-'=>' ',
	   );
	   foreach($unicode as $nonUnicode=>$uni) $str = preg_replace("/($uni)/i",$nonUnicode,$str);
	   return $str;
	}
	
?>	